<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qqmlobjectmodel.cpp -->
  <title>ObjectModel QML 类型| Qt QML 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtqml-index.html">Qt QML</a></td><td ><a href="qtqml-qmlmodule.html">QML Types</a></td><td >ObjectModel QML 类型</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#properties">属性</a></li>
<li class="level1"><a href="#attached-properties">附加属性</a></li>
<li class="level1"><a href="#methods">方法</a></li>
<li class="level1"><a href="#details">详细信息</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title"> QML中的ObjectModel类型</h1>
<span class="subtitle"></span>
<!-- $$$ObjectModel-brief -->
<p>定义一组要用作模型的项。<a href="#details">更多…</a></p>
<!-- @@@ObjectModel -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> 导入语句:</td><td class="memItemRight bottomAlign"> import QtQml.Models 2.12</td></tr></table></div><ul>
<li><a href="qml-qtqml-models-objectmodel-members.html">所有成员的列表，包括继承的成员</a></li>
</ul>
<a name="properties"></a>
<h2 id="properties">属性</h2>
<ul>
<li class="fn"><b><b><a href="qml-qtqml-models-objectmodel.html#count-prop">count</a></b></b> : int</li>
</ul>
<a name="attached-properties"></a>
<h2 id="attached-properties">附加属性</h2>
<ul>
<li class="fn"><b><b><a href="qml-qtqml-models-objectmodel.html#index-attached-prop">index</a></b></b> : int</li>
</ul>
<a name="methods"></a>
<h2 id="methods">方法</h2>
<ul>
<li class="fn"><b><b><a href="qml-qtqml-models-objectmodel.html#append-method">append</a></b></b>(object <i>item</i>)</li>
<li class="fn"><b><b><a href="qml-qtqml-models-objectmodel.html#clear-method">clear</a></b></b>()</li>
<li class="fn">object <b><b><a href="qml-qtqml-models-objectmodel.html#get-method">get</a></b></b>(int <i>index</i>)</li>
<li class="fn"><b><b><a href="qml-qtqml-models-objectmodel.html#insert-method">insert</a></b></b>(int <i>index</i>,  object <i>item</i>)</li>
<li class="fn"><b><b><a href="qml-qtqml-models-objectmodel.html#move-method">move</a></b></b>(int <i>from</i>,  int <i>to</i>, <i> int n = 1</i>)</li>
<li class="fn"><b><b><a href="qml-qtqml-models-objectmodel.html#remove-method">remove</a></b></b>(int <i>index</i>, <i> int n = 1</i>)</li>
</ul>
<!-- $$$ObjectModel-description -->
<a name="details"></a>
<h2 id="details">详细信息</h2>
<p><a href="qml-qtqml-models-objectmodel.html">ObjectModel</a>包含要在视图中使用的可视项。当在视图中使用<a href="qml-qtqml-models-objectmodel.html">ObjectModel</a>时，视图不需要委托，因为<a href="qml-qtqml-models-objectmodel.html">ObjectModel</a>已经包含了可视委托(项)。</p>
<p>项目可以通过<a href="qml-qtqml-models-objectmodel.html#index-attached-prop">index</a> 附加属性确定其在模型中的索引。</p>
<p>下面的示例在<a href="../qtquick/qml-qtquick-listview.html">ListView</a>中放置了三个彩色矩形。</p>
<pre class="cpp">

  import <span class="type">QtQuick</span> <span class="number">2.0</span>
  import <span class="type">QtQml</span><span class="operator">.</span>Models <span class="number">2.1</span>

  Rectangle {
      ObjectModel {
          id: itemModel
          Rectangle { height: <span class="number">30</span>; width: <span class="number">80</span>; color: <span class="string">&quot;red&quot;</span> }
          Rectangle { height: <span class="number">30</span>; width: <span class="number">80</span>; color: <span class="string">&quot;green&quot;</span> }
          Rectangle { height: <span class="number">30</span>; width: <span class="number">80</span>; color: <span class="string">&quot;blue&quot;</span> }
      }

      ListView {
          anchors<span class="operator">.</span>fill: parent
          model: itemModel
      }
  }

</pre>
<p class="centerAlign"><img src="images/objectmodel.png" alt="" /></p><p><b>参见</b><a href="../qtquick/qtquick-views-example.html">Qt Quick例子 - 视图</a>。</p>
<!-- @@@ObjectModel -->
<h2>属性文档</h2>
<!-- $$$count -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="count-prop">
<td class="tblQmlPropNode"><p>
<a name="count-prop"></a><span class="name">count</span> : <span class="type"><a href="qml-int.html">int</a></span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>模型中的项数。此属性是只读的。</p>
</div></div><!-- @@@count -->
<br/>
<h2>附加属性文档</h2>
<!-- $$$index -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="index-attached-prop">
<td class="tblQmlPropNode"><p>
<a name="index-attached-prop"></a><span class="name">ObjectModel.index</span> : <span class="type"><a href="qml-int.html">int</a></span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>此附加属性保存模型中此委托项的索引。</p>
<p>它附加到委托的每个实例。</p>
</div></div><!-- @@@index -->
<br/>
<h2>方法文档</h2>
<!-- $$$append -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="append-method">
<td class="tblQmlFuncNode"><p>
<a name="append-method"></a><span class="name">append</span>(<span class="type">object</span> <i>item</i>)</p></td></tr>
</table></div>
</div><div class="qmldoc"><p>在模型的末尾追加一个新项。</p>
<pre class="cpp">

  objectModel<span class="operator">.</span>append(objectComponent<span class="operator">.</span>createObject())

</pre>
<p>该方法在Qt 5.6中引入。</p>
<p><b>请参见</b><a href="qml-qtqml-models-objectmodel.html#insert-method">insert()</a> 和 <a href="qml-qtqml-models-objectmodel.html#remove-method">remove()</a>。</p>
</div></div><!-- @@@append -->
<br/>
<!-- $$$clear -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="clear-method">
<td class="tblQmlFuncNode"><p>
<a name="clear-method"></a><span class="name">clear</span>()</p></td></tr>
</table></div>
</div><div class="qmldoc"><p>从模型中清除所有项。</p>
<p>该方法在Qt 5.6中引入。</p>
<p><b>参见</b><a href="qml-qtqml-models-objectmodel.html#append-method">append()</a> 和 <a href="qml-qtqml-models-objectmodel.html#remove-method">remove()</a>。</p>
</div></div><!-- @@@clear -->
<br/>
<!-- $$$get -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="get-method">
<td class="tblQmlFuncNode"><p>
<a name="get-method"></a><span class="type">object</span> <span class="name">get</span>(<span class="type"><a href="qml-int.html">int</a></span> <i>index</i>)</p></td></tr>
</table></div>
</div><div class="qmldoc"><p>返回模型中的以 <i>index</i>为索引项。这允许从JavaScript访问或修改项: </p>
<pre class="cpp">

  Component<span class="operator">.</span>onCompleted: {
      objectModel<span class="operator">.</span>append(objectComponent<span class="operator">.</span>createObject())
      console<span class="operator">.</span>log(objectModel<span class="operator">.</span>get(<span class="number">0</span>)<span class="operator">.</span>objectName);
      objectModel<span class="operator">.</span>get(<span class="number">0</span>)<span class="operator">.</span>objectName <span class="operator">=</span> <span class="string">&quot;first&quot;</span>;
  }

</pre>
<p><i>index</i>必须是列表中的一个元素。</p>
<p>该方法在Qt 5.6中引入。</p>
<p><b>参见</b><a href="qml-qtqml-models-objectmodel.html#append-method">append()</a>。</p>
</div></div><!-- @@@get -->
<br/>
<!-- $$$insert -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="insert-method">
<td class="tblQmlFuncNode"><p>
<a name="insert-method"></a><span class="name">insert</span>(<span class="type"><a href="qml-int.html">int</a></span> <i>index</i>,  <span class="type">object</span> <i>item</i>)</p></td></tr>
</table></div>
</div><div class="qmldoc"><p>在位置<i>index</i>处向模型插入一个新项。</p>
<pre class="cpp">

  objectModel<span class="operator">.</span>insert(<span class="number">2</span><span class="operator">,</span> objectComponent<span class="operator">.</span>createObject())

</pre>
<p><i>index</i>必须指向列表中现有的项，或列表末尾之后的项(相当于append)。</p>
<p>该方法在Qt 5.6中引入。</p>
<p><b>参见</b><a href="qml-qtqml-models-objectmodel.html#append-method">append()</a> 和 <a href="qml-qtqml-models-objectmodel.html#remove-method">remove()</a>。</p>
</div></div><!-- @@@insert -->
<br/>
<!-- $$$move -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="move-method">
<td class="tblQmlFuncNode"><p>
<a name="move-method"></a><span class="name">move</span>(<span class="type"><a href="qml-int.html">int</a></span> <i>from</i>,  <span class="type"><a href="qml-int.html">int</a></span> <i>to</i>, <i> int n = 1</i>)</p></td></tr>
</table></div>
</div><div class="qmldoc"><p>将<i>n</i> 个项目<i>from</i> （从）一个位置移动<i>to</i> （到）另一个位置。</p>
<p>from和to范围必须存在;例如，将前3项移动到模型的末尾:</p>
<pre class="cpp">

  objectModel<span class="operator">.</span>move(<span class="number">0</span><span class="operator">,</span> objectModel<span class="operator">.</span>count <span class="operator">-</span> <span class="number">3</span><span class="operator">,</span> <span class="number">3</span>)

</pre>
<p>该方法在Qt 5.6中引入。</p>
<p><b>参见</b><a href="qml-qtqml-models-objectmodel.html#append-method">append()</a>。</p>
</div></div><!-- @@@move -->
<br/>
<!-- $$$remove -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="remove-method">
<td class="tblQmlFuncNode"><p>
<a name="remove-method"></a><span class="name">remove</span>(<span class="type"><a href="qml-int.html">int</a></span> <i>index</i>, <i> int n = 1</i>)</p></td></tr>
</table></div>
</div><div class="qmldoc"><p>从模型中删除<i>index</i>项。</p>
<p>该方法在Qt 5.6中引入。</p>
<p><b>参见</b><a href="qml-qtqml-models-objectmodel.html#clear-method">clear()</a>。</p>
</div></div><!-- @@@remove -->
<br/>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
